package leetcode.editor.offer;

// 83. 删除排序链表中的重复元素:https://leetcode.cn/problems/remove-duplicates-from-sorted-list/submissions/
public class Solution18II {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    // 注意不相等就需要cur一直往后移动
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null) return head;
            ListNode pre = head, cur = head.next;
            while (cur != null) {
                if (pre.val != cur.val) {
                    pre.next = cur;
                    pre = pre.next;
                }

                cur = cur.next;
            }

            pre.next = null;
            return head;
        }
    }
}
